﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SignInServer.Server.Configuration;

namespace SignInServer.Server.File
{
    /// <summary>
    /// 用于储存来自客户端的文件或读取客户端请求的文件,一下为旧方法，仅供参考，等待更新
    /// </summary>
    class BaseFile
    {
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName;
        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string ExtensionName;
        /// <summary>
        /// 文件总大小
        /// </summary>
        public int DataSize;
        /// <summary>
        /// 文件类型
        /// </summary>
        public int FileType;
        /// <summary>
        /// 发送文件路径
        /// </summary>
        public string FilePath;
        /// <summary>
        /// socket传输文件个数
        /// </summary>
        public int Count;
        /// <summary>
        /// 传输位置
        /// </summary>
        public int StartIndex;
        /// <summary>
        /// 写入写出数据
        /// </summary>
        public FileStream fi;

        /// <summary>
        /// 发送文件数据构造函数
        /// </summary>
        public BaseFile(string path, int type)
        {
            if (fi == null)
            {
                FileType = type;

                FilePath = path;
                FileName = Path.GetFileNameWithoutExtension(path);
                ExtensionName = Path.GetExtension(path);
                StartIndex = 1;
                fi = new FileStream(path, FileMode.Open);

                DataSize = (int)fi.Length;
                Count = DataSize / SocketConfiguration.PACK_SIZE + 1;

                fi.Seek(0, SeekOrigin.Begin);
            }
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>读取的文件</returns>
        public byte[] SendFileData()
        {
            if (StartIndex > Count)
            {
                return null;
            }

            byte[] data;
            if (StartIndex == Count)
            {
                data = new byte[DataSize - ((StartIndex - 1) * SocketConfiguration.PACK_SIZE)];
                fi.Read(data, 0, data.Length);

                fi.Close();
                Console.WriteLine("文件传送完毕");
            }
            else
            {
                data = new byte[SocketConfiguration.PACK_SIZE];
                fi.Read(data, 0, data.Length);

            }
            ++StartIndex;
            return data;
        }

        string AcceptFilePath;
        /// <summary>
        /// 接受文件数据构造函数
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="extensionname">文件扩展名</param>
        /// <param name="datasize">文件总大小</param>
        /// <param name="count">文件总个数</param>
        public BaseFile(int type, int uid, int number, string name, string extensionname, int count)
        {
            //AcceptFilePath = FileDataSpecification.getPath(type, uid, number, name, extensionname);
            Count = count;
            StartIndex = 1;
            fi = null;
        }

        /// <summary>
        /// 接收文件
        /// </summary>
        /// <param name="data">文件数据</param>
        /// <returns>是否接受完全数据</returns>
        public bool AcceptFileData(byte[] data)
        {
            if (fi == null)
            {
                fi = new FileStream(AcceptFilePath, FileMode.Create);
            }

            fi.Write(data, 0, data.Length);

            Console.WriteLine("写入文件数据:" + data.Length);

            if (StartIndex == Count)
            {
                fi.Close();

                fi = null;

                Console.WriteLine("接收完成");

                return true;
            }

            Console.WriteLine("接收文件数+1，还需:" + (Count - StartIndex) + "个数据包");

            ++StartIndex;

            return false;

        }
    }
}
